// function flat(arr) {
//     while (arr.some(item => Array.isArray(item))) {
//         arr = [].concat(...arr);
//     }
//     return arr;
// }



// const res = flat([1, 2, [3, 4, [10, 20, [100, 200]]], 5]);
// console.log(res);

//数组降维  去掉null 【】 和 undefined（字节笔试题）
let arr = [
    [
        [
            [
                [0]
            ],
            [1]
        ],
        [
            [
                [2],
                [3]
            ]
        ],
        [
            [4],
            [5]
        ]
    ]
];
let arr2 = [
    ['a', 'b'],
    [0, ['a']],
    [false],
    []
];
let res = [1, 2, [3, 4, [10, 20, [100, 200]]], 5];
Array.prototype.myFlatten = function() {
    return this.reduce((pre, cur, index) => {
        if (cur == undefined) return pre;
        else {
            return Array.isArray(cur) ? pre.concat(cur.myFlatten()) : pre.concat(cur);
        }
    }, [])
}

let array = arr.myFlatten();
let array2 = arr2.myFlatten();
let array3 = res.myFlatten();
console.log(array);
console.log(array2);
console.log(array3);

//https://www.jianshu.com/p/541b84c9df90 reduce用法